import requests
class Cinema:
    # 初始化登录，获取authorization参数，方便后续操作
    def __init__(self):
        self.session = requests.Session()
        url = 'http://api.51tickets.com/login'
        body = {"username": "s001", "password": "123456", "code": "1357", "uuid": "8e90a8c2e43649a2830d49fca947a715"}
        resp = self.session.post(url=url,json=body)
        token = resp.json()['token']
        headers = {'authorization':'Bearer '+token}
        self.session.headers.update(headers)

    # 根据id查询影院信息
    def search_cinemaInfor_by_id(self,id):
        url = 'http://api.51tickets.com/system/cinema/'+str(id)
        res = self.session.get(url = url)
        return res.json()
    # 根据影院名称查询影院信息 （无效）
    # 根据详细地址查询影院信息 （不能模糊查询）
    def search_cinemaInfor_by_cinemaNameOrcinemaLocation(self,pageNum,pageSize,cinemaName,cinemaLocation):
        url = 'http://api.51tickets.com/system/cinema/list'
        # if cinemaLocation == None:
        #     params = {"pageNum":pageNum,"pageSize":pageSize}
        # else:
        #     params = {"pageNum": pageNum, "pageSize": pageSize,'cinemaLocation':cinemaLocation}
        if cinemaName is not None:
            if cinemaLocation is not None:
                params = {'pageNum':pageNum,'pageSize':pageSize,'cinemaName':cinemaName,'cinemaLocation':cinemaLocation}
            else:
                params = {'pageNum': pageNum, 'pageSize': pageSize, 'cinemaName': cinemaName}
        elif cinemaLocation is not None:
            params = {'pageNum': pageNum, 'pageSize': pageSize,'cinemaLocation': cinemaLocation}
        else:
            params = {'pageNum': pageNum, 'pageSize': pageSize}
        resp = self.session.get(url=url,params=params)
        return resp.json()

    # 保存影院信息
    def add_cinemaInfo(self,cinemaName,cinemaPhone,cinemaLocation):
        url = 'http://api.51tickets.com/system/cinema'
        data = {"cinemaId":None,
                "cinemaName":cinemaName,
                "cinemaPhone":cinemaPhone,
                "cinemaArea":2703,
                "cinemaLocation":cinemaLocation,
                "cinemaBrand":4,
                "cinemaImg":None,
                "cinemaCity":321,
                "cinemaLng":None,
                "cinemaLat":None,
                "cinemaProvince":25
                }
        resp = self.session.post(url = url,json=data)
        return resp.json()

    # 修改影院信息
    def update_cinemaInfo(self,cinemaId,cinemaName,cinemaPhone,cinemaLocation,cinemaArea=2703,cinemaBrand=11,cinemaCity=321,cinemaProvince=25):
        url = 'http://api.51tickets.com/system/cinema'
        data = {"searchValue":None,
                "createBy":None,
                "createTime":None,
                "updateBy":None,
                "updateTime":None,
                "remark":None,
                "params":{},
                "cinemaId":cinemaId,
                "cinemaName":cinemaName,
                "cinemaPhone":cinemaPhone,
                "cinemaArea":cinemaArea,
                "cinemaLocation":cinemaLocation,
                "cinemaBrand":cinemaBrand,
                "cinemaImg":None,
                "cinemaCity":cinemaCity,
                "cinemaLng":None,
                "cinemaLat":None,
                "cinemaProvince":cinemaProvince
                }
        resp = self.session.put(url = url,json=data)
        return resp.json()

    # 删除影院信息
    def delete_cinemaInfo(self,cinemaId):
        url = f'http://api.51tickets.com/system/cinema/{cinemaId}'
        resp = self.session.delete(url=url)
        return resp.json()

    # 上传图片
    def upload_img_cinemaInfo(self,path):
        url = 'http://api.51tickets.com/common/upload'
        fp = open(path,mode='rb')
        files = {'file':fp}
        resp = self.session.post(url=url,files = files)
        return resp.json()

if __name__ == '__main__':
    c1 = Cinema()
    # print(c1.search_cinemaInfor_by_id(4))
    # print(c1.search_cinemaInfor_by_cinemaLocation(1, 10, "广富林路692号松江万达广场5楼"))
    # print(c1.add_cinemaInfo('哈哈哈影城','188456','美丽湖南'))
    # print(c1.update_cinemaInfo(34,'牛逼666','177283','美丽河南'))
    # print(c1.delete_cinemaInfo(33))
    # print(c1.upload_img_cinemaInfo('../imgs/b.jpg'))
    print(c1.search_cinemaInfor_by_cinemaNameOrcinemaLocation(1,10,"hahahah","ddd"))